//
//  APIService+Test.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/24.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "APIService+Test.h"
#import "APIService+Training.h"

@implementation APIService (Test)

- (RACSignal *)submitSpineTestWithUserId:(NSString *)objectId
                              NeckInjury:(int)neckInjury
                             waistInjury:(int)waistInjury
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        BmobQuery * query = [BmobQuery queryWithClassName:@"SpineTest"];
        [query whereKey:@"userId" equalTo:self.user.objectId];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if ([array count]) {
                BmobObject *object = [array objectAtIndex:0];
                if (object)
                {
                    if (neckInjury != 100) {
                        [object setObject:@(neckInjury)     forKey:@"neckPoint"];
                    }
                    if (waistInjury != 100) {
                        [object setObject:@(waistInjury)     forKey:@"waistPoint"];
                    }
                    [object updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        if (isSuccessful) {
                            if (neckInjury != 100) {
                                self.spineTestData.neckPoint = neckInjury;
                            }
                            if (waistInjury != 100) {
                                self.spineTestData.waistPoint = waistInjury;
                            }
                            if (self.spineTestData.neckPoint < 100 && self.spineTestData.waistPoint < 100) {
                                [[[APIService shareService] addNewTrainingPlan] subscribeNext:^(id x) {
                                    [subscriber sendNext:x];
                                }];
                            }
                            else {
                                [subscriber sendNext:@(YES)];
                            }
                        }
                        else {
                            [MBProgressHUD showQuickTipWithTitle:nil withText:error.localizedDescription];
                        }
                    }];
                    
                }
            }
            else {
                BmobObject *object = [BmobObject objectWithClassName:@"SpineTest"];
                [object setObject:objectId          forKey:@"userId"];
                [object setObject:@(neckInjury)     forKey:@"neckPoint"];
                [object setObject:@(waistInjury)    forKey:@"waistPoint"];
                [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                    if (isSuccessful) {
                        [self updateSpineTest:1];
                        self.spineTestData = [[SpineTestData alloc] init];
                        self.spineTestData.neckPoint = neckInjury;
                        self.spineTestData.waistPoint = waistInjury;
                        if (neckInjury != 100) {
                            self.spineTestData.neckPoint = neckInjury;
                        }
                        if (waistInjury != 100) {
                            self.spineTestData.waistPoint = waistInjury;
                        }
                        if (self.spineTestData.neckPoint < 100 && self.spineTestData.waistPoint < 100) {
                            [[[APIService shareService] addNewTrainingPlan] subscribeNext:^(id x) {
                                [subscriber sendNext:x];
                            }];
                        }
                        else {
                            [subscriber sendNext:@(YES)];
                        }
                    }
                }];
            }
        }];
        return nil;
    }];
}

- (void)updateSpineTest:(int)hasTested
{
    BmobUser *user = [BmobUser getCurrentUser];
    [user setObject:@(hasTested) forKey:@"hadSpineTested"];
    [user updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        NSLog(@"error = %@",error);
    }];
}

@end
